package com.dwxt.cdms.applets.service;

import com.dwxt.cdms.dao.EquipmentMapper;
import com.dwxt.cdms.entity.Customer;
import com.dwxt.cdms.entity.Equipment;
import com.dwxt.cdms.entity.EquipmentCustom;
import com.dwxt.cdms.exception.CustomException;
import com.dwxt.cdms.service.CustomerService;
import com.dwxt.cdms.service.EquipmentCustomService;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yanlong
 * @version 1.0
 * @className EquipService
 * @date 2021/5/10 20:17
 */
@Service
public class EquipService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private EquipmentCustomService equipmentCustomService;

    @Autowired
    private CustomerService customerService;

    @Transactional
    public BaseResult bind(String serialNumber, Integer customerId, String category,Integer managerId,String name) {
        Customer manager = customerService.selectCustomerById(managerId);
        Equipment equipment = equipmentMapper.selectEquipmentBySerialNumber(serialNumber, null);
        if (equipment != null) {
            List<Equipment> equipmentList = equipmentCustomService.selectEquipmentByCustomerId(customerId);
            //boolean isAble = judgeBindEquipment(equipmentList, equipment);
            if (!equipmentCustomService.judgeBindEquipment(equipmentList, equipment)) {
                throw new CustomException(StatusCode.YES_BIND_EQUIPMENT);
            }
        } else {
            equipment = new Equipment();
            equipment.setId(SnowIdUtil.getInstance().nextId())
                    .setCategory(category)
                    .setSerialNumber(serialNumber)
                    .setCreateTime(LocalDateTime.now())
                    .setType(VariableConstants.STRING_CONSTANT_0)
                    .setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setIsEnabled(VariableConstants.STRING_CONSTANT_1)
                    .setName(name);
            if (manager.getStationId() != null) {
                equipment.setStationId(manager.getStationId());
            }
            equipmentMapper.insert(equipment);
        }

        EquipmentCustom equipmentCustom = new EquipmentCustom();
        equipmentCustom.setId(SnowIdUtil.getInstance().nextId())
                .setCreateTime(LocalDateTime.now())
                .setCustomerId(customerId)
                .setEquipmentId(equipment.getId())
                .setIsDelete(VariableConstants.STRING_CONSTANT_0);

        this.equipmentMapper.updateDistributionStatusToOne(equipment.getId());
        equipmentCustomService.insert(equipmentCustom);

        return BaseResult.ok();
    }


    public BaseResult selectByCustomerId(Integer customerId) {
        List<Equipment> equipmentList = equipmentCustomService.selectEquipmentByCustomerId(customerId);
        List<Equipment> collect = equipmentList.stream().sorted(Comparator.comparing(Equipment::getCreateTime).reversed()).collect(Collectors.toList());
        return BaseResult.ok(collect);
    }
}
